package cn.tedu.ivos.dict.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.dict.mapper.DictMapper;
import cn.tedu.ivos.dict.pojo.dto.DictQuery;
import cn.tedu.ivos.dict.pojo.dto.DictSaveParam;
import cn.tedu.ivos.dict.pojo.entity.Dict;
import cn.tedu.ivos.dict.pojo.vo.DictVO;
import cn.tedu.ivos.dict.service.DictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class DictServiceImpl implements DictService {
    @Autowired
    DictMapper dictMapper;

    @Override
    public List<DictVO> selectDict(DictQuery query) {
        log.debug("查询字典："+query);
        List<DictVO> list =dictMapper.selectDict(query);
        return list;
    }

    @Override
    public void saveDict(DictSaveParam param) {
        log.debug("添加字典业务参数：{}", param);
        Dict dict=new Dict();
        BeanUtils.copyProperties(param,dict);
        if (dict.getId()==null) {
            dict.setStatus("1");
            dict.setCreateTime(new Date());
            dictMapper.saveDict(dict);
        } else {
            dict.setUpdateTime(new Date());
            dictMapper.updateDict(dict);
        }
    }

    @Override
    public void deleteDict(Long id) {
        log.debug("删除字典：id="+id);
      Dict dict =new Dict();
      dict.setId(id);
      dict.setStatus("0");
      dict.setUpdateTime(new Date());
      dictMapper.updateDict(dict);

    }
}
